﻿using Futuret.Core.Utils;
using Futuret.Core.Utils.SM;
using Futuret.Sys.IService.Sys;
using Futuret.Sys.IService.Sys.Dto;
using Futuret.Sys.Model.Entity.Sys;
using Mapster;
using SqlSugar;

namespace Futuret.Sys.Service.Sys;

/// <summary>
///     Author: Riva
///     Date: 2023/7/11 14:44
///     Description:
/// </summary>
public class SysUserService : BaseSysService<SysUser>, ISysUserService
{
    /// <summary>
    ///     新增
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Add(SysUserParamDto input)
    {
        //TODO 可以SM2解密前端发送过来的密码 然后在加密数据
        var entity = input.Adapt<SysUser>();
        entity.Signature = IdUtil.GetStringId();
        entity.Password = EncryptUitl.Hmacsha256Encryption(input.Password ?? "123456", entity.Signature);

        await InsertAsync(entity);
    }

    /// <summary>
    ///     编辑
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Edit(SysUserParamDto input)
    {
        const string publicKey =
            "041d3f635ae42bad0ae352ef9f1d8ae437608f7f254e14acf758aed19d0aa61afa3cc74625ad61ba98db64959696fe20544f11d263a4f63debd7a357f1407ae950";
        const string privateKey = "00c5b20ed1d44c648f03ef61da801a7532a699638e0574214db2d904309e963edd";
        const string text = "this is password";
        //生成签名
        var sing = EncryptUitl.Sm2Sign(publicKey, privateKey, text);
        Console.WriteLine(sing);
        var verify = EncryptUitl.Sm2VerifySign(text, sing, publicKey, privateKey);
        Console.WriteLine(verify);
        var enc = EncryptUitl.Sm2Encrypt(text, publicKey, privateKey);
        Console.WriteLine(enc);
        var doc = EncryptUitl.Sm2Decrypt(enc, publicKey, privateKey);
        Console.WriteLine(doc);

        await Task.CompletedTask;
    }

    /// <summary>
    ///     删除
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<bool> Delete(BaseInputIds input)
    {
        // TODO 业务错误403客户端展示给用户
        throw new FuturetException("请输入用户名！");
        await Task.CompletedTask;
        return true;
    }

    /// <summary>
    ///     分页
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<List<SysUser>> Page(SysUserParamDto input)
    {
        return await GetListAsync();
    }
}